package com.xyh.service.impl;


import com.xyh.bean.ManagerClass;
import com.xyh.dao.IClassDao;
import com.xyh.dao.impl.ClassDaoImpl;
import com.xyh.service.IClassService;
import com.xyh.util.PageUtil;
import com.xyh.vo.ClassVO;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: XiongYuhao
 * @Date: 2020/10/26 0026-上午 12:01
 * @Description: MyForestManagerSystem
 * @version：1.0
 */
public class ClassServiceImpl implements IClassService {
    IClassDao icd = new ClassDaoImpl();
    @Override
    public int getId(String areaName) {
        return icd.getId(areaName);
    }

    @Override
    public PageUtil<ClassVO> listByPage(int pageNo, int pageSize, String className, String managerArea) {
        int counts = icd.classCounts(className,managerArea);
        List<ManagerClass> mcs = icd.classListByPage(pageNo,pageSize,className, managerArea);
        List<ClassVO> classVO = new ArrayList<>();
        for (ManagerClass mc: mcs){
            ClassVO cvo = new ClassVO();
            try {
                BeanUtils.copyProperties(cvo,mc);
                BeanUtils.copyProperty(cvo,"areaName",mc.getArea().getAreaName());
                BeanUtils.copyProperty(cvo,"treeType",mc.getArea().getTreeType());
                BeanUtils.copyProperty(cvo,"land",mc.getArea().getLand());
                BeanUtils.copyProperty(cvo,"advTree",mc.getArea().getAdvTree());
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            classVO.add(cvo);
        }
        PageUtil<ClassVO> pageBean = new PageUtil<>(classVO,pageNo,pageSize,counts);
        return pageBean;
    }

    @Override
    public boolean updateClass(ManagerClass mc) {
        return icd.updateClass(mc)>0?true:false;
    }

    @Override
    public boolean addClass(ManagerClass mc) {
        return icd.addClass(mc)>0?true:false;
    }
}
